 /Android|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent) ? window.location.href ="m-index.html" : ""  ;


var cookieKey = 'userdata';
var data = {

}



function getData(){
	if($.cookie(cookieKey)){
		var cookieData = JSON.parse($.cookie(cookieKey));
		return cookieData ;	
	}else{
		return data
	}

}

$(function(){
	function updateCookie(){
		var cookieData = JSON.stringify(data);
		$.cookie(cookieKey,cookieData);
		calc();
		console.log(data);
	}

	$('.btn-detail .close').click(function(event) {
		$(this).hide();
		$(this).siblings('.open').show();
		
		$(this).siblings('dd').slideDown();

	});
	$('.btn-detail .open').click(function(event) {
		$(this).hide();
		$(this).siblings('.close').show();
		$(this).siblings('dd').slideUp();
	});
	$('#consent').change(function(event) {
		if($(this).is(':checked')){
			$('.js-htmlmeilsend').addClass('active')
		}else{
			$('.js-htmlmeilsend').removeClass('active')
		}

	})

	$(window).on('scroll load',function(){
		sidebarPosition();
	})


	//处理sidebar
	var slidebarFixed = $('.js-slide-fixed');
	var slidebarFixedTop = slidebarFixed.offset().top;
	var sidebar = $('.sidebar');

	var sidebarTop = sidebar.offset().top;


	var scrollStart = document.documentElement.scrollTop;
	
	function sidebarPosition(){
		var limit = 20;
		var sidebarH = sidebar.height();
		//var moveSidebarTop = sidebar.offset().top;
		var bodyScroll = document.documentElement.scrollTop ;
		var totalWrapH = $('.form-right').innerHeight();
		var totalWrapT = $('.form-right').offset().top;
		var sidebarPosiT = sidebar.position().top;
		// console.log(bodyScroll);
		//bodyScroll = parseInt(bodyScroll);
		//console.log(bodyScroll);
		//console.log(sidebar.get(0).getBoundingClientRect().top);
		//向下滚动
		//console.log(totalWrapH);
		if(bodyScroll > (slidebarFixedTop - limit)){
			if((sidebarH +  bodyScroll - sidebarTop)<=totalWrapH){
				sidebar.css({top:bodyScroll - sidebarTop + limit  +'px'});
			}
		}else{
			sidebar.css({top:0 +'px'});
		}

		return ;
		// if(bodyScroll-scrollStart>0){
		// 	if(bodyScroll >= (sidebarTop - limit)){
		// 		if((sidebarH+moveSidebarTop)>=(lastWrapH+lastWrapT)){

		// 			sidebar.css({top:moveSidebarTop - sidebarTop +'px'});
		// 		}else{
		// 			var relativeTop = bodyScroll - sidebarTop + limit;
		// 			relativeTop = relativeTop;
		// 			sidebar.css({top:relativeTop+'px'});
		// 		}

		// 	}else{
		// 		sidebar.css({top:0+'px'})
		// 	}

		// }else if(bodyScroll-scrollStart<0){
		// 	if(bodyScroll >= (sidebarTop - limit)){
		// 		var relativeTop = bodyScroll - sidebarTop + limit;
		// 		relativeTop = parseInt(relativeTop);
		// 		sidebar.css({top:relativeTop+'px'});
		// 	}else{
		// 		sidebar.css({top:0+'px'})
		// 	}
		// }

	}



	//处理右边表单滑动
	$('.scrollNext , input[name=phone_num]').on('click',function(){
		scrollNext($(this));
	})


	function scrollNext(_$) {
		var next = _$.parents('.step-wrap').next('.step-wrap');
		var limit = 20;
		if(next.length>0){
			next.css('display','block');
			next.css('display');
			next.css('opacity',1);
			var nextTop = next.offset().top - limit;
			//console.log(nextTop);
			$('html,body').animate({
				scrollTop:nextTop
			},500);
		}

	}

	//跳转到结果;
	function scrollResult() {
		var limit = 20;
		var nextTop = $('#result').offset().top - limit;
			$('html,body').animate({
				scrollTop:nextTop
			},500);
		calc();//计算选择之后的费用结果
	}

	//按钮点击控制变化控制
	btnCtl()
	function btnCtl(){
		$('#housetype-simple').click(function(){
			$('.js-housetype').text('戸建て');
			data.housetype = [
				'simple',
				'戸建て'
			];
			updateCookie();
			showSidebar(1);
			$('input[name=i_sim_housetype]').val('戸建て');
		})
		$('#housetype-complex').click(function(event) {
			$('.js-housetype').text('集合住宅');
			data.housetype = [
				'complex',
				'集合住宅'
			];
			updateCookie();
			showSidebar(1);
			$('input[name=i_sim_housetype]').val('集合住宅');
		});

		//手机处理

		$('#phone_num-yes').click(function(){
			//scrollNext($('label[for=phone_service-yes]'));
			//$('.phone_num_list').slideDown();
			data.carrier = [
				1,
				'持っている'
			];
			$('.js-carrier').text('持っている');
			updateCookie();
			showSidebar(2);
		})
		// $('input[name=phone_num]').on('click',function(e){
		// 		var val = parseInt($(this).val());
		// 		$('.js-phone_num').text(val+'台');
		// 		//设置手机多少台
		// 		data.carrier = [
		// 			1,
		// 			val
		// 		];
		// 		console.log(val);
		// 		updateCookie();
		// 		showSidebar(2);
		// 		//设置详细表单
		// 		//$('input[name=i_sim_phone_num]').val(val);
		// 		//$('label[for=phone_service-yes]').click();
				
		// });

		$('#phone_num-no').click(function(){
			$('.js-toggle-contents').slideUp();
			$('.js-carrier').text('持っていない');
			data.carrier = [
				0,
				'持っている'
			];
			updateCookie();
			showSidebar(2);
		})

		$('#phone_service-yes').on('click',function() {
			
			$('.js-phone_service').text('利用する');
			data.phone_service = [
				1,
				'利用する'
			];
			$('input[name=i_sim_tel]').val('利用する');
			updateCookie();
			showSidebar(3);
		});	

		$('#phone_service-no').click(function() {
			$('.js-phone_service').text('利用しない');
			data.phone_service = [
				0,
				'利用しない'
			];
			$('input[name=i_sim_tel]').val('利用しない');
			updateCookie();
			showSidebar(3);
		});

		$('#tv_service-yes').click(function() {
			$('.js-tv_service').text('利用する');
			data.tv_service = [
				1,
				'利用する'
			];
			$('input[name=i_sim_tv]').val('利用する');
			updateCookie();
			showSidebar(4);
		});	
		$('#tv_service-no').click(function() {
			$('.js-tv_service').text('利用しない');
			data.tv_service = [
				0,
				'利用しない'
			];
			$('input[name=i_sim_tv]').val('利用しない');
			updateCookie();
			showSidebar(4);
		});

		$('#security_service-yes').click(function() {
			$('.js-security_service').text('利用する');
			data.security_service = [
				1,
				'利用する'
			];
			$('input[name=i_sim_security]').val('利用する');
			updateCookie(); 
			showSidebar(5);
			scrollResult()
		});	
		$('#security_service-no').click(function() {
			$('.js-security_service').text('利用しない');
			data.security_service = [
				0,
				'利用しない'
			];
			$('input[name=i_sim_security]').val('利用する');
			updateCookie(); 
			showSidebar(5);
			scrollResult()
		});		

		$('#mail_result-yes').click(function() {
			$('.js-toggle-contents').slideDown();
			data.mail_result = [
				1,
				'利用する'
			];
			$('input[name=htmlmailaddress]').val('利用する');
			updateCookie(); 
			$('.mail-detail').slideDown();
		});	
		$('#mail_result-no').click(function() {
			data.mail_result = [
				0,
				'利用しない'
			];
			$('input[name=htmlmailaddress]').val('利用する');
			updateCookie(); 
			$('.mail-detail').slideUp('slow/400/fast',function() {
				scrollResult();
			});		
		});
	//housetype-complex

	}
	function showSidebar(num){
		$('.js-side-column').eq(num).fadeIn();
	}


	$(".js-modal-window-handle").colorbox({
		inline:true, 
		width:"50%",
		scrolling:true,
		height:"100%",
		closeButton:false
	});
	$('.js-cbox-close').click(function() {
		$('#cboxOverlay').click();
	})

	//初始化将之前选择过的选项显示出来
	init();
	function init(){
		data =  getData();
		console.log(data)
		if(data==undefined){
			return;
		}
		if(data.housetype!=undefined){
			$('.js-housetype').text(data.housetype[1]);
			data.housetype[0] == 'simple' ? $('#housetype-simple').get(0).checked=true : $('#housetype-complex').get(0).checked=true;
		}
		if(data.carrier!=undefined){
			if(data.carrier[0]==0){
				$('.js-carrier').text('持っていない');
				$('#phone_num-no').get(0).checked = true;
			}
			if(data.carrier[0]==1){
				$('.phone_num_list').toggle();
				$('.js-carrier').text('持っている');
				$('.js-phone_num').text(data.carrier[1]);
				$('#phone_num-yes').get(0).checked = true;
				var num = data.carrier[1];
				console.log('.phone_num-'+num);
				//$('#phone_num-'+num).get(0).checked = true;
			}
			$('#step-phone-num').css({
				display:'block',
				opacity:1
			})
			showSidebar(1);
		}
		if(data.phone_service!=undefined){
			data.phone_service[0]  ? $('#phone_service-yes').get(0).checked=true : $('#phone_service-no').get(0).checked=true;
			$('.js-phone_service').text(data.phone_service[1]);
			$('#step-phone_service').css({
				display:'block',
				opacity:1
			})
			showSidebar(2);
		}
		if(data.tv_service!=undefined){
			data.tv_service[0]  ? $('#tv_service-yes').get(0).checked=true : $('#tv_service-no').get(0).checked=true;
			$('.js-tv_service').text(data.tv_service[1]);
			$('#step-tv_service').css({
				display:'block',
				opacity:1
			})
			showSidebar(3);
		}
		if(data.security_service!=undefined){
			data.security_service[0]  ? $('#security_service-yes').get(0).checked=true : $('#security_service-no').get(0).checked=true;
			$('.js-security_service').text(data.security_service[1]);
			$('#step-security_service').css({
				display:'block',
				opacity:1
			})
			showSidebar(3);
		}
		if(data.mail_result!=undefined){
			data.mail_result[0]  ? $('#mail_result-yes').get(0).checked=true : $('#mail_result-no').get(0).checked=true;
			$('.js-mail_result').text(data.mail_result[1]);
			$('#step-mail_result').css({
				display:'block',
				opacity:1
			})
			showSidebar(3);
		}
	}
	calc();
	function calc(){
		data = getData();
		// if(typeof data.housetype == 'undefined') return;
		// if(typeof data.carrier == 'undefined') return;
		// if(typeof data.phone_service == 'undefined') return;
		// if(typeof data.tv_service == 'undefined') return;
		// if(typeof data.security_service == 'undefined') return;

		var housetype = data.housetype ? data.housetype[0] :'';
		var carrier = data.carrier ?  data.carrier[0] : '';
		var phone_service = data.phone_service ? data.phone_service[0] :'';
		var security_service = data.security_service ? data.security_service[0] :'';
		var tv_service = data.tv_service ? data.tv_service[0] :'';

		var temText,
			nomalSum = 0,
			firstSum = 0,
			secondSum = 0;

		if(housetype=='simple'){
			//累计计算各月数值
			nomalSum += calcData.nomal.internet_fee.simple;
			firstSum +=calcData.first.internet_fee.simple;
			secondSum += calcData.second.internet_fee.simple;

			$('#js-internet-price-nomal').text(calcData.nomal.internet_fee.simple+'円/月');

			temText = '初月無料' ;

			$('#js-internet-price-1st').text(temText);	

			temText = calcData.second.internet_fee.simple+'円/月';

			$('#js-internet-price-2nd').text(temText);

			// //设置折扣
			// nomalSum += calcData.nomal.discount_cp.simple;
			// temText = calcData.nomal.discount_cp.simple_zero_word;
			// $('#js-cp-price-nomal').text(temText);
			// firstSum += calcData.first.discount_cp.simple;
			// temText = calcData.first.discount_cp.simple+'円/月';
			// $('#js-cp-price-1st').text(temText);
			// secondSum += calcData.second.discount_cp.simple;
			// temText = calcData.second.discount_cp.simple+'円/月';
			// $('#js-cp-price-2nd').text(temText);

		}else if(housetype=='complex'){
			//累计计算各月数值

			nomalSum +=calcData.nomal.internet_fee.complex;
			firstSum +=calcData.first.internet_fee.complex;
			secondSum +=calcData.second.internet_fee.complex;

			$('#js-internet-price-nomal').text(calcData.nomal.internet_fee.complex+'円/月');
			temText =  '初月無料' ;
			$('#js-internet-price-1st').text(temText);	
			temText = calcData.second.internet_fee.complex+'円/月';
			$('#js-internet-price-2nd').text(temText);

			// //设置折扣
			// nomalSum += calcData.nomal.discount_cp.complex;
			// temText = calcData.nomal.discount_cp.complex_zero_word;
			// $('#js-cp-price-nomal').text(temText);

			// firstSum += calcData.first.discount_cp.complex;
			// temText = calcData.first.discount_cp.complex+'円/月';
			// $('#js-cp-price-1st').text(temText);
			// secondSum += calcData.second.discount_cp.complex;
			// temText = calcData.second.discount_cp.complex+'円/月';
			// $('#js-cp-price-2nd').text(temText);

		}
		//取消手机数量参与计算
		// //加入有添加电话数量
		// if(carrier){
		// 	//判断是否有电话服务，有才添加显示
		// 	if(phone_service){
		// 		var phone_num = data.carrier[1];
		// 		nomalSum += calcData.nomal.discount_phone;
		// 		temText = calcData.nomal.discount_phone_zero_word;
		// 		$('#js-smartvalue-price-nomal').text(temText);

		// 		firstSum += calcData.first.discount_phone * phone_num ;
		// 		temText = calcData.first.discount_phone * phone_num +'円';
		// 		$('#js-smartvalue-price-1st').text(temText);

		// 		secondSum += calcData.second.discount_phone * phone_num ;
		// 		temText = calcData.second.discount_phone * phone_num +'円';
		// 		$('#js-smartvalue-price-2nd').text(temText);

		// 		$('#js-smartvalue-price-view').css('display','table-row');
		// 	}else{
		// 		$('#js-smartvalue-price-view').hide();
		// 	}
		// }
		if(phone_service){
			nomalSum += calcData.nomal.tel_service.selected ;
			firstSum +=calcData.first.tel_service.selected ;
			secondSum +=calcData.second.tel_service.selected ;

			nomalSum += calcData.nomal.discount ;
			firstSum +=calcData.first.discount ;
			secondSum +=calcData.second.discount ;

			//处理选择电话费用字段
			temText  = calcData.nomal.tel_service.selected + '円/月';
			$('#js-phone-price-nomal').text(temText);
			
			//temText  = '' ;
			$('#js-phone-price-1st').text(temText);
			
			//temText  = '' ;
			$('#js-phone-price-2nd').text(temText);

			$('#js-phone-price-view').css('display','table-row');

			$('#js-constuction-price-view').hide();//选择电话的时候  工事代隐藏

			//处理选择电话时的折扣
			$('#js-discount-view').css('display','table-row');

			temText  = calcData.nomal.discount + '円/月';

			$('#js-cp-price-nomal').text(temText);
			temText  = calcData.first.discount + '円/月';
			//temText  = '' ;
			$('#js-cp-price-1st').text(temText);
			temText  = calcData.second.discount + '円/月';
			//temText  = '' ;
			$('#js-cp-price-2nd').text(temText);
			

		}else{
			nomalSum += calcData.nomal.construction_fee;
			firstSum +=calcData.first.construction_fee;
			secondSum +=calcData.second.construction_fee;

			temText  = calcData.nomal.construction_fee + '円/月';

			$('#js-phone-price-view').hide();
			$('#js-discount-view').hide(); //没有选择电话时隐藏折扣

			$('#js-constuction-price-nomal').text(temText);
			$('#js-constuction-price-1st').text(temText);
			$('#js-constuction-price-2nd').text(temText);

			$('#js-constuction-price-view').css('display','table-row');
		}



		// if(tv_service){
		// 	nomalSum += calcData.nomal.tv_service;
		// 	temText = calcData.nomal.tv_service+'円';
		// 	$('#js-tv-price-nomal').text(temText);

		// 	firstSum += calcData.first.tv_service;
		// 	temText = calcData.first.tv_service_zero_word;
		// 	$('#js-tv-price-1st').text(temText);

		// 	secondSum += calcData.second.tv_service;
		// 	temText = calcData.second.tv_service_zero_word;
		// 	$('#js-tv-price-2nd').text(temText);

		// 	$('#js-tv-price-view').css('display','table-row');
		// }else{
		// 	$('#js-tv-price-view').hide();
		// }

		if(security_service){
			nomalSum += calcData.nomal.security_fee;
			temText = calcData.nomal.security_fee+'円/月';
			$('#js-security-price-nomal').text(temText);

			firstSum += calcData.first.security_fee;

			temText = '24ヶ月無料';

			$('#js-security-price-1st').text(temText);

			secondSum += calcData.second.security_fee;
			temText = '24ヶ月無料';
			$('#js-security-price-2nd').text(temText);

			$('#js-security-price-view').css('display','table-row');
		}else{
			$('#js-security-price-view').hide();
		}

		console.log(nomalSum)
		console.log(firstSum)
		console.log(secondSum)

		//最后结果
		$('#js-real-price-nomal ,#js-result_internet_cost').text(nomalSum+'円');
		$('#js-result_internet_cost').text(nomalSum);
		$('#js-real-price-1st').text((firstSum < 0 ? 0 : firstSum) +'円');
		$('#js-real-price-2nd , #js-result-price').text((secondSum < 0 ? 0 : secondSum) +'円');
		$('#js-result-price').text((secondSum < 0 ? 0 : secondSum) );
	}

	 $("#form1").validate({
	 	rules:{
	 		i_todo:"required",
	 		i_name:"required",
	 		i_kana:"required",
	 		i_zip:"required",
	 		i_pref:"required",
	 		i_add1:"required",
	 		i_add2:"required",
	 		i_tel:{
	 			required:true,
	 			minlength:10,
	 			maxlength:11,
	 		},

	 	},
	 	errorPlacement: function(error, element) { 

	 		element.parents('.input-box').find('.pop-require').show();
	 		element.parents('.input-box').find('.pop-require').append(error);
   			// error.appendTo(element.next(''));  
		},
		success: function(label) {
			console.log(label);
			label.parents('.input-box').find('.pop-require').hide();			
			//label.addClass("valid").text("Ok!")
		}
	 });

	 $('.js-htmlmeilsend').click(function(event) {
	 		var email = $('#i_mail_simresult').val();
	 		var reg = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/;
	 		if(reg.test(email)){
	 			var html = $('.price-breakdown-table').prop("outerHTML");
	 			console.log(html);
	 			$.ajax({
	 				url: 'email.php',
	 				type: 'POST',
	 				data: {html:html,email:email,result:data,client:'pc'},
	 			})
	 			.done(function(res) {
	 				if(res){
	 					alert('Mail sent successfully');
	 				}else{
	 					alert('Failed to send the email. Please contact the administrator');
	 				}
	 			})
	 			.fail(function() {
	 				
	 			})
	 			.always(function() {
	 				console.log("complete");
	 			});
	 			
	 		}else{
	 			 $('#i_mail_simresult').focus();
	 		}

	 });

 }) 

//Jquery结尾

//   $(window).on("lo	ad resize scroll", function() {
//   	var bodyScroll = document.documentElement.scrollTop ;
	// bodyScroll = parseInt(bodyScroll);
	// console.log(bodyScroll);
//   });
 



//console.log($.cookie(cookieKey));

